package ru.ifmo.hmm;

import java.io.*;
import java.util.*;

public class Main {
	public static void main(String[] args) throws IOException {
		Locale.setDefault(Locale.US);
		Scanner in = new Scanner(new File("input.txt"));
		Util.Model m = Util.readModel(in);
		int k = in.nextInt();
		int[] d = new int[k];
		for (int i = 0; i < k; i++) {
			d[i] = in.nextInt() - 1;
		}
		double dp = Util.dataProbability(m, d);
		System.out.println(dp);
		int[] seq = Util.mostProbableSequence(m, d);
		for (int s : seq)
			System.out.print((s + 1) + " ");
		System.out.println();
		for (int t = 0; t < k; t++)
			System.out.print((Util.mostProbableState(m, d, t) + 1) + " ");
		System.out.println();
	}
}